<!--
 * @Author: ch cwl_ch@163.com
 * @Date: 2022-12-19 17:45:07
 * @LastEditors: ch
 * @LastEditTime: 2023-01-03 15:31:46
 * @Description: file content
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
  <title>-</title>
</head>
<script type="text/javascript" src="./uniwebview.js"></script>
<script type="text/javascript">

const identity = 2;
const getQuery = ()=>{
    let queryArr = window.location.search.substr(1).split('&');
    let obj = {};
    queryArr.forEach(item => {
        const arr = item.split('=');
        obj[arr[0]] = decodeURIComponent(arr[1]);
    })
    return obj;
}
let routerQuery = getQuery();
let source = null;

    if(window.EventSource){
        console.info("此浏览器支持SSE");
        // 连接的建立
        source = new EventSource(`${routerQuery.uri}/connect?userId=${routerQuery.userId}&identity=${identity}`);

        // 监听服务的推送的消息
        source.addEventListener("message",function (e){
			let content = e.data;
			console.log(content)
            setMessageContent(content);
        });

    }else {
        setMessageContent("此浏览器不支持");
    }

    function setMessageContent(content){
        uni.postMessage({
            data:  JSON.parse(content)
        });
    }

    function sourceClose(){
        console.info("close方法执行");
        // 客户端source的关闭
        source.close();
        // 服务端map的移除
        httpRequest = new XMLHttpRequest();
        httpRequest.open("get","http://localhost:9000/close?userId="+userId+"&identity="+identity);
        httpRequest.send();
    }
</script>
</html>
